<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-10-18
 * Time: 13:59
 */

namespace app\manage\admin;
use app\admin\controller\Admin;
use app\common\builder\ZBuilder; // 引入ZBuilder
use app\common\model\Process as ProcessModel;
use app\common\model\ProcessType as ProcessTypeModel;
use think\Db;
use think\App;
class Process extends Admin
{

    public $processType = '';
    public $group = '';
    public $post = '';
    public function __construct(App $app = null)
    {
        $this->processType = ProcessTypeModel::column('name', 'id');
        $this->group = \app\common\model\Group::whereIn('pid',['0','1'])->column('name', 'id');
        $this->post = \app\common\model\Post::where('status','1')->column('title', 'id');
        parent::__construct($app);
    }


    //审批流程列表
    public function lists()
    {
        // 读取数据
        $map = $this->getMap();
        // 排序
        $order = $this->getOrder();
        // 数据列表
        //$data_list = ProcessModel::where($map)->order($order)->paginate();
        $data_list = Db::name('qt_process')
            ->alias('p')
            ->leftJoin('qt_process_type pt', 'pt.id = p.process_id')
            ->field('p.*,pt.name')
            ->where($map)
            ->order('p.created_time desc')
            ->paginate()->each(function ($item){
                $item['first_post_name'] = Db::name('qt_post')->where('id',$item['first_post_id'])->value('title');
                $item['secede_post_name'] = Db::name('qt_post')->where('id',$item['secede_post_id'])->value('title');
                $item['third_post_name'] = Db::name('qt_post')->where('id',$item['third_post_id'])->value('title');
                return $item;
            });
        // 分页数据
        $page = $data_list->render();
        $btn_access1 = [
            'title' => '编辑',
            'icon'  => 'fa fa-fw fa-pencil',
            'class' => 'btn btn-xs btn-default',
            'href'  => url('edit', ['id' => '__id__']),
        ];
        $btn_access = [
            'title' => '新增审批流程',
            'icon'  => 'fa fa-fw fa-plus',
            'class' => 'btn btn-info',
            'href'  => url('add'),
        ];
        $btn_access2 = [
            'title' => '删除审批流程',
            'icon'  => 'fa fa-fw fa-trash-o',
            'class' => 'btn btn-xs btn-default ajax-get confirm',
            'href'  => url('delete', ['id' => '__id__']),
        ];
        //设置页面
        return ZBuilder::make('table')
            ->hideCheckbox()
            ->addColumn('id', '编号', 'text')
            ->addColumn('title', '审批流程名称', 'text')
            ->addColumn('name', '审批分类', 'text')
            ->addColumn('first_post_name', '第一审批岗位', 'text')
            ->addColumn('secede_post_name', '第二审批岗位', 'text')
            ->addColumn('third_post_name', '第三审批岗位', 'text')
            ->addColumn('sort', '排序', 'text')
            ->addColumn('status', '状态', 'status','',['1'=>'显示','2'=>'隐藏'])
            ->addColumn('created_time', '创建日期', 'datetime','','Y-m-d H:i:s')
            ->addColumn('right_button','操作','btn')
            ->setColumnWidth([
                'id'  => 50,
                'created_time'=> 130,
                'right_button'=>80
            ])
            ->setTableName('qt_notic') // 指定数据表名
            ->addTopButton('custom',$btn_access) // 添加顶部按钮
            ->addRightButton('edit',$btn_access1) // 添加右侧按钮
            ->addRightButton('delete',$btn_access2) // 添加右侧按钮
            ->addOrder('id,title,create_time,update_time')
            ->setPrimaryKey('id') // 设置主键名为username
            ->setPageTitle('审批流程列表')
            ->setRowList($data_list) // 设置表格数据
            ->setPages($page) // 设置分页数据
            ->fetch();
    }
    //添加审批流程
    public function add(){
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            $result = $this->validate($data, 'Process.add');
            if(true !== $result) $this->error($result);
            $data['created_time'] = time();
            if (ProcessModel::create($data)) {
                $this->success('新增成功', 'lists');
            } else {
                $this->error('新增失败');
            }
        }else
        {
            return ZBuilder::make('form')
                ->addFormItems(
                    [
                        ['text:7','title','审批名称','<code>请填写审批名称</code>'],
                        ['select:7','process_id','审批分类','<code>请选择审批分类</code>',$this->processType],
                        ['select:7','group_id','公司团队','<code>请选择公司团队</code>',$this->group],
                        ['select:7','post_id','岗位','<code>请选择公司岗位</code>',$this->post],

                        ['Linkage:7','first_post_id','第一审批岗位','<code>请选择第一审批岗位</code>',$this->post,'',url('get_first_group'),'first_user_id'],
                        ['select:7','first_user_id','第一审批人'],

                        ['Linkage:7','secede_post_id','第二审批岗位','<code>请选择第二审批岗位</code>',$this->post,'',url('get_secede_group'),'secede_user_id'],
                        ['select:7','secede_user_id','第二审批人'],

                        ['Linkage:7','third_post_id','第三审批岗位','<code>请选择第二审批岗位</code>',$this->post,'',url('get_third_group'),'third_user_id'],
                        ['select:7','third_user_id','第三审批人'],

                        ['radio:7','replace','是否需要替岗','',['1'=>'需要','2'=>'不需要'],'1'],
                        ['radio', 'status', '状态', '', [1=>'显示', 2=>'隐藏'],'1'],
                        ['text:7','sort','排序','<code>值越小越靠前,1-100</code>','50'],
                    ]
                )
                ->setPageTitle('添加审批流程')
                ->fetch();
        }
    }

    public function get_first_group($first_post_id){
        $arr['code'] = '1'; //判断状态
        $arr['msg'] = '请求成功'; //回传信息
        $data= Db::name('qt_users')->where('post_id',$first_post_id)->field('id,nickname')->select(); //数据
        $result = [];
        foreach ($data as $key=>$value){
            $result[$key]['key'] = $value['id'];
            $result[$key]['value'] = $value['nickname'];
        }
        $arr['list'] = $result;
        return json($arr);
    }

    public function get_secede_group($secede_post_id){
        $arr['code'] = '1'; //判断状态
        $arr['msg'] = '请求成功'; //回传信息
        $data= Db::name('qt_users')->where('post_id',$secede_post_id)->field('id,nickname')->select(); //数据
        $result = [];
        foreach ($data as $key=>$value){
            $result[$key]['key'] = $value['id'];
            $result[$key]['value'] = $value['nickname'];
        }
        $arr['list'] = $result;
        return json($arr);
    }


    public function get_third_group($third_post_id){
        $arr['code'] = '1'; //判断状态
        $arr['msg'] = '请求成功'; //回传信息
        $data= Db::name('qt_users')->where('post_id',$third_post_id)->field('id,nickname')->select(); //数据
        $result = [];
        foreach ($data as $key=>$value){
            $result[$key]['key'] = $value['id'];
            $result[$key]['value'] = $value['nickname'];
        }
        $arr['list'] = $result;
        return json($arr);
    }


    //编辑审批流程
    public function edit($id=null){
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Process.add');
            if(true !== $result) $this->error($result);
            if (ProcessModel::update($data)) {
                $this->success('编辑成功', 'lists');
            } else {
                $this->error('编辑失败');
            }
        }else
        {
            $info = ProcessModel::get($id);
            return ZBuilder::make('form')
                ->addFormItems(
                    [
                        ['hidden','id','id'],
                        ['text:7','title','审批名称','<code>请填写审批名称</code>'],
                        ['select:7','process_id','审批分类','<code>请选择审批分类</code>',$this->processType],
                        ['select:7','group_id','公司团队','<code>请选择公司团队</code>',$this->group],
                        ['Linkage:7','first_post_id','第一审批岗位','<code>请选择第一审批岗位</code>',$this->post,'',url('get_first_group'),'first_user_id'],
                        ['select:7','first_user_id','第一审批人'],
                        ['Linkage:7','secede_post_id','第二审批岗位','<code>请选择第二审批岗位</code>',$this->post,'',url('get_secede_group'),'secede_user_id'],
                        ['select:7','secede_user_id','第二审批人'],
                        ['Linkage:7','third_post_id','第三审批岗位','<code>请选择第二审批岗位</code>',$this->post,'',url('get_third_group'),'third_user_id'],
                        ['select:7','third_user_id','第三审批人'],
                        ['radio:7','replace','是否需要替岗','',['1'=>'需要','2'=>'不需要']],
                        ['radio', 'status', '状态', '', [1=>'显示', 2=>'隐藏']],
                        ['text:7','sort','排序','<code>值越小越靠前,1-100</code>'],
                    ]
                )
                ->setFormData($info)
                ->setPageTitle('编辑公告')
                ->fetch();
        }
    }
    //删除审批流程
    public function delete($id=null){
        if ($id === null) $this->error('缺少参数');
        try{
            Db::startTrans();
            //删除审批流程
            Db::name('qt_process')->where('id',$id)->delete();
            Db::commit();
        }catch (Exception $exception){
            Db::rollback();
            $this->error('系统繁忙请稍后再试','lists');
        }
        $this->success('删除成功','lists');

    }



}